package view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import entity.ChargeRule;
import entity.ParkReceipt;
import entity.PassBill;
import main.Main;
import util.Constants;
import util.DateTimeUtils;


public class AddParkReceiptJPanel extends JPanel {
	private static final long serialVersionUID = 8549141458642551032L;

	public AddParkReceiptJPanel(PassBill bill){
		setLayout(null);
        initUI(bill);
	}
	
	private void initUI(PassBill bill){
		
		JLabel lab1 = new JLabel("车牌号",SwingConstants.RIGHT);
		lab1.setFont(Constants.font);
		lab1.setBounds(100, 30, 60, 30);

        JLabel lab1input = new JLabel("",SwingConstants.LEFT);
        lab1input.setFont(Constants.font);
        lab1input.setBounds(165, 30, 240, 30);
        
        
        JLabel lab2 = new JLabel("车位号",SwingConstants.RIGHT);
        lab2.setFont(Constants.font);
        lab2.setBounds(100, 80, 60, 30);
        
        JLabel lab2input = new JLabel("",SwingConstants.LEFT);
        lab2input.setFont(Constants.font);
        lab2input.setBounds(165, 80, 240, 30);
        

        JLabel lab3 = new JLabel("入场时间",SwingConstants.RIGHT);
        lab3.setFont(Constants.font);
        lab3.setBounds(100, 130, 60, 30);
        
        JLabel lab3input = new JLabel("",SwingConstants.LEFT);
        lab3input.setFont(Constants.font);
        lab3input.setBounds(165, 130, 240, 30);
        
        
        JLabel lab4 = new JLabel("离场时间",SwingConstants.RIGHT);
        lab4.setFont(Constants.font);
        lab4.setBounds(100, 180, 60, 30);
        
        JLabel lab4input = new JLabel("",SwingConstants.LEFT);
        lab4input.setFont(Constants.font);
        lab4input.setBounds(165, 180, 240, 30);

        
        JLabel lab5 = new JLabel("计费时长",SwingConstants.RIGHT);
        lab5.setFont(Constants.font);
        lab5.setBounds(100, 230, 60, 30);
        
        JLabel lab5input = new JLabel("",SwingConstants.LEFT);
        lab5input.setFont(Constants.font);
        lab5input.setBounds(165, 230, 240, 30);
        
        
        JLabel lab6 = new JLabel("金额",SwingConstants.RIGHT);
        lab6.setFont(Constants.font);
        lab6.setBounds(100, 280, 60, 30);
        
        JLabel lab6input = new JLabel("",SwingConstants.LEFT);
        lab6input.setFont(Constants.font);
        lab6input.setBounds(165, 280, 240, 30);
        
        
        JLabel lab7 = new JLabel("创建时间",SwingConstants.RIGHT);
        lab7.setFont(Constants.font);
        lab7.setBounds(100, 330, 60, 30);
        
        JLabel lab7input = new JLabel("",SwingConstants.LEFT);
        lab7input.setFont(Constants.font);
        lab7input.setBounds(165, 330, 240, 30);
		
        
        ChargeRule rule = new ChargeRule();
		ParkReceipt receipt = new ParkReceipt();
		receipt.carId = bill.carId;
		receipt.parkId = bill.parkId;
		receipt.entryIime = bill.entryIime;
		receipt.leaveIime = DateTimeUtils.dtNow();
		long deltaMinutes = DateTimeUtils.diffMinutes(receipt.entryIime,receipt.leaveIime);		
		receipt.stockCount = (int)(deltaMinutes / 60 + (deltaMinutes % 60 == 0 ? 0 : 1));
		receipt.money = rule.computeGeneral(receipt.parkId, receipt.entryIime.substring(0, 10), receipt.stockCount);
		receipt.createTime = receipt.leaveIime;
		
		lab1input.setText(receipt.carId);
		lab2input.setText(receipt.parkId);
		lab3input.setText(receipt.entryIime);
		lab4input.setText(receipt.leaveIime);
		lab5input.setText(receipt.stockCount + " 小时");
		lab6input.setText(receipt.money);
		lab7input.setText(receipt.createTime);
		
		
        JButton btn = new JButton("收费");
		btn.setFont(Constants.font);
		btn.setBounds(165, 380, 60, 30);        
		btn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (bill.leave() && receipt.save()) {
					JOptionPane.showMessageDialog(null, "操作成功！");
					Main.dlgStack.pop().dispose();
				}else{
					JOptionPane.showMessageDialog(null, "操作失败！");
				}
			}
		});
		
		add(lab1);
		add(lab1input);
		add(lab2);
		add(lab2input);
		add(lab3);
		add(lab3input);
		add(lab4);
		add(lab4input);
		add(lab5);
		add(lab5input);
		add(lab6);
		add(lab6input);
		add(lab7);
		add(lab7input);
		add(btn);
	}

}